<template>
  <div class="page-scan-mana">
    <el-form :inline="true" :model="dataQueryForm" ref="dataQueryForm">
      <div class="pc-page-header">
        <el-form-item label="">
          <el-button type="primary" @click="refresh('dataQueryForm')">刷新</el-button>
          <el-button type="primary" @click="viewEditDescFn">编辑说明</el-button>
          <el-button type="primary" @click="viewEditIndexFn(getDefaultIndexInfo(), '新增进口量', '新增', 1)">新增</el-button>
        </el-form-item>
      </div>
    </el-form>

    <!--列表数据-->
    <egrid border class="page-list"
           :data="tableData"
           :columns="columns"
           :columns-schema="columnsSchema"
           :columns-props="columnsProps">
    </egrid>

    <!--分页组件-->
    <div class="pc-pagination-panel">
      <pc-pagination ref="pcPagination" :url="pageUrl.queryDataList" :queryData="dataQueryForm"
                     @on-query="queryCallBackFn">
      </pc-pagination>
    </div>

    <!--编辑说明-->
    <pc-dialog :is-show="isShowEditDescDialog" title='编辑说明' @on-close="isShowEditDescDialog = false">
      <div>
        <edit-desc indexType="1" @sure="afterEditDesc()">
        </edit-desc>
      </div>
    </pc-dialog>

    <!--新增/编辑指数-->
    <pc-dialog :is-show="isShowEditIndexDialog" :title='dialogTitle' @on-close="isShowEditIndexDialog = false">
      <div>
        <edit-index :indexInfo="indexInfo" :btnTitle="btnTitle" :isDatePickerActive="isDatePickerActive" @sure="afterEditIndex()">
        </edit-index>
      </div>
    </pc-dialog>

  </div>
</template>

<script type="text/ecmascript-6">
  import PcPagination from 'components/form/pagination';
  import PcButton from 'components/form/button';
  import PcDialog from 'components/dialog';
  import EgridCellView from 'components/wms/comm/EgridCellView.vue';
  import {enums} from '../../../../utils/settings';
  import EditIndex from './editIndex.vue';
  import EditDesc from './editDesc.vue';
  let that;

  // 操作按钮
  var Btn = {
    template: `<div style="display:flex; width: 100%;flex-wrap: wrap">
        <el-button type="primary" plain @click="gotoEditIndex(row)">编辑</el-button>
        <el-button type="info"    plain @click="gotoDeleteIndex(row)">删除</el-button>
      </div>`,
    props: ['row'],
    computed: {
    },
    methods: {
      // 跳转编辑指数
      gotoEditIndex (val) {
        this.$emit('edit-index', val);
      },
      // 跳转删除指数
      gotoDeleteIndex (val) {
        this.$emit('delete-index', val);
      }
    }
  };

  export default {
    data() {
      return {
        indexInfo: null,
        editType: '',
        dialogTitle: '',
        btnTitle: '',
        isDatePickerActive: false,
        isShowEditIndexDialog: false,
        isShowEditDescDialog: false,
        dataQueryForm: {
          indexType: enums.SK_INDEX_TYPE.JKL
        },
        tableData: [],
        columns: [
          {
            label: '统计月份',
            prop: 'statisticsDateYM'
          },
          {
            label: '进口量（吨）',
            prop: 'indexValueDisplay'
          },
          {
            label: '操作人',
            prop: 'lastOperator'
          },
          {
            label: '操作时间',
            prop: 'updateTime'
          },
          {
            label: '操作',
            minWidth: 100,
            fixed: 'right',
            component: Btn, // 'el-button'
            listeners: {
              'edit-index' (data, title) {
                that.viewEditIndexFn(data, '编辑进口量', '保存', enums.SK_INDEX_EDIT_TYPE.EDIT);
              },
              'delete-index' (data) {
                that.deleteIndexFn(data);
              }
            },
            propsHandler: function ({ row, col, column }) {
              return {row, col, column};
            }
          }
        ],
        columnsSchema: {
        },
        selecetedRows: [],
        // columnsProps 用于定义所有 columns 公共的属性，
        // 这里属性可以参考这里： http://element.eleme.io/#/zh-CN/component/table#table-column-attributes
        columnsProps: {
          height: 40,
          minWidth: 140,
          showOverflowTooltip: true,
          component: EgridCellView
        },
        // columnsHandler 可用于在现有的 columns 进行操作，对 columns 进行增删改，这里新增了操作列
        pageUrl: {
          queryDataList: '/malisk/server/macro/queryMacroIndexList'
        }
      };
    },
    components: {
      PcPagination, PcButton, PcDialog, EditIndex, EditDesc
    },
    created() {
      that = this;
    },

    methods: {
      // 表头样式
      getHeaderStyle({row, column, rowIndex, columnIndex}) {
        if (rowIndex === 0) {
          return 'background: #f5f5f5; height: 50px';
        } else {
          return '';
        }
      },
      getRowClass({row, column, rowIndex, columnIndex}) {
        if (rowIndex === 0) {
          return 'background:rgb(245, 245, 245);';
        } else {
          return '';
        }
      },
      // 获取默认指数新增信息
      getDefaultIndexInfo() {
        let defaultIndexInfo = {id: '', statisticsDate: ''};
        // 获取上个月的月份
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth();
        if (month === 0) {
          year = year - 1;
          month = 12;
        }
        defaultIndexInfo.statisticsDate = year + '-' + month;
        return defaultIndexInfo;
      },
      viewEditDescFn () {
        this.isShowEditDescDialog = true;
      },
      viewEditIndexFn (scopeRow, dialogTitle, btnTitle, editType) {
        this.indexInfo = scopeRow;
        this.dialogTitle = dialogTitle;
        this.btnTitle = btnTitle;
        this.editType = editType;
        this.isDatePickerActive = (editType === enums.SK_INDEX_EDIT_TYPE.EDIT);
        this.isShowEditIndexDialog = true;
      },
      deleteIndexFn (val) {
        // 调用后端接口
        this.ajaxBox({
          title: '操作确认',
          message: '确定要删除该指数吗？',
          type: 'POST',
          url: '/malisk/server/macro/deleteMacroIndex',
          data: {
            id: val.id,
            version: val.version
          },
          loading: true,
          success: (res) => {
            setTimeout(() => {
              this.$pcNProgress.done();
            }, 300);
            if (res.code === 200) {
              this.successTip('删除成功', 2000);
              this.$refs.pcPagination.queryListWithPageFn(false);
            } else {
              this.errorTip(res.message);
            }
          }
        });
      },
      openMessage() {
        this.$notify.info({
          title: '自定义位置',
          message: '右下角弹出的消息',
          position: 'bottom-right'
        });
      },
      afterEditDesc() {
        this.isShowEditDescDialog = false;
      },
      afterEditIndex() {
        this.isShowEditIndexDialog = false;
        if (this.editType === enums.SK_INDEX_EDIT_TYPE.ADD) {
          this.$refs.pcPagination.queryListWithPageFn(true);
        } else {
          this.$refs.pcPagination.queryListWithPageFn(false);
        }
      },
      queryCallBackFn(res) {
        this.tableData = res.result.list;
      },
      queryDataFn() {
        this.$refs.pcPagination.queryListWithPageFn(true);
      },
      refresh(formName) {
        this.$refs[formName].resetFields();
        this.dataQueryForm.indexType = enums.SK_INDEX_TYPE.JKL;
        this.queryDataFn();
      }
    }
  };
</script>
<style>
</style>
